﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Threading.Tasks;

namespace FlashCards.SL.Controls
{
    public class Tile : Expander
    {

        public static readonly DependencyProperty NormalWidthProperty;
        public static readonly DependencyProperty ExpandedWidthProperty;

        public static readonly DependencyProperty NormalHeightProperty;
        public static readonly DependencyProperty ExpandedHeightProperty;

        public double NormalWidth
        {
            get { return (double)GetValue(NormalWidthProperty); }
            set { SetValue(NormalWidthProperty, value); }
        }

        public double ExpandedWidth
        {
            get { return (double)GetValue(ExpandedWidthProperty); }
            set { SetValue(ExpandedWidthProperty, value); }
        }

        public double NormalHeight
        {
            get { return (double)GetValue(NormalHeightProperty); }
            set { SetValue(NormalHeightProperty, value); }
        }

        public double ExpandedHeight
        {
            get { return (double)GetValue(ExpandedHeightProperty); }
            set { SetValue(ExpandedHeightProperty, value); }
        }

        static Tile()
        {
            NormalWidthProperty = DependencyProperty.Register("NormalWidth", typeof(double), typeof(Tile), null);
            ExpandedWidthProperty = DependencyProperty.Register("ExpandedWidth", typeof(double), typeof(Tile), null);

            NormalHeightProperty = DependencyProperty.Register("NormalHeight", typeof(double), typeof(Tile), null);
            ExpandedHeightProperty = DependencyProperty.Register("ExpandedHeight", typeof(double), typeof(Tile), null);
        }

        public Tile()
        {
            
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var expandBtn = GetTemplateChild("ExpanderButton") as Button;
            expandBtn.Click += new RoutedEventHandler(expandBtn_Click);

            var collapseBtn = GetTemplateChild("CollapseButton") as Button;
            collapseBtn.Click += new RoutedEventHandler(collapseBtn_Click);

            this.Expanded += (o, e) =>
            { GoToExpandedState(); };
            this.Collapsed += (o, e) =>
            { GoToCollapsedState(); };

            if (IsExpanded)
                GoToExpandedState();
        }

        void expandBtn_Click(object sender, RoutedEventArgs e)
        {
            IsExpanded = true;
        }

        void collapseBtn_Click(object sender, EventArgs e)
        {
            IsExpanded = false;
        }

        void GoToExpandedState()
        {
            var anim = GetTemplateChild("WidthExpandAnimation") as DoubleAnimation;
            if (anim == null)
                return;
            anim.To = ExpandedWidth;

            anim = GetTemplateChild("HeightExpandAnimation") as DoubleAnimation;
            anim.To = ExpandedHeight;

            var stb = GetTemplateChild("ExpandStoryboard") as Storyboard;
            stb.Begin();

            Task t = new Task(() =>
            {
                Dispatcher.BeginInvoke(() =>
                VisualStateManager.GoToState(this, "Expanded", false));
            });

            t.Start();
        }

        void GoToCollapsedState()
        {
            

            var anim = GetTemplateChild("WidthExpandAnimation") as DoubleAnimation;
            if (anim == null)
                return;

            anim.To = NormalWidth;

            anim = GetTemplateChild("HeightExpandAnimation") as DoubleAnimation;
            anim.To = NormalHeight;

            var stb = GetTemplateChild("ExpandStoryboard") as Storyboard;
            stb.Begin();

            VisualStateManager.GoToState(this, "Normal", false);
        }

    }
}
